package archive

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/archive"
	archiveReq "github.com/flipped-aurora/gin-vue-admin/server/model/archive/request"
	"gorm.io/gorm"
)

type ArchiveLocationService struct {
}

// CreateArchiveLocation 创建档案存放位置记录
// Author [piexlmax](https://github.com/piexlmax)
func (archiveLocationService *ArchiveLocationService) CreateArchiveLocation(archiveLocation *archive.ArchiveLocation) (err error) {
	err = global.GVA_DB.Create(archiveLocation).Error
	return err
}

// DeleteArchiveLocation 删除档案存放位置记录
// Author [piexlmax](https://github.com/piexlmax)
func (archiveLocationService *ArchiveLocationService) DeleteArchiveLocation(ID string, userID uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&archive.ArchiveLocation{}).Where("id = ?", ID).Update("deleted_by", userID).Error; err != nil {
			return err
		}
		if err = tx.Delete(&archive.ArchiveLocation{}, "id = ?", ID).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// DeleteArchiveLocationByIds 批量删除档案存放位置记录
// Author [piexlmax](https://github.com/piexlmax)
func (archiveLocationService *ArchiveLocationService) DeleteArchiveLocationByIds(IDs []string, deleted_by uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&archive.ArchiveLocation{}).Where("id in ?", IDs).Update("deleted_by", deleted_by).Error; err != nil {
			return err
		}
		if err := tx.Where("id in ?", IDs).Delete(&archive.ArchiveLocation{}).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// UpdateArchiveLocation 更新档案存放位置记录
// Author [piexlmax](https://github.com/piexlmax)
func (archiveLocationService *ArchiveLocationService) UpdateArchiveLocation(archiveLocation archive.ArchiveLocation) (err error) {
	err = global.GVA_DB.Model(&archive.ArchiveLocation{}).Where("id = ?", archiveLocation.ID).Updates(&archiveLocation).Error
	return err
}

// GetArchiveLocation 根据ID获取档案存放位置记录
// Author [piexlmax](https://github.com/piexlmax)
func (archiveLocationService *ArchiveLocationService) GetArchiveLocation(ID string) (archiveLocation archive.ArchiveLocation, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&archiveLocation).Error
	return
}

// GetArchiveLocationInfoList 分页获取档案存放位置记录
// Author [piexlmax](https://github.com/piexlmax)
func (archiveLocationService *ArchiveLocationService) GetArchiveLocationInfoList(info archiveReq.ArchiveLocationSearch) (list []archive.ArchiveLocation, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&archive.ArchiveLocation{})
	var archiveLocations []archive.ArchiveLocation
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Narchive_no != "" {
		db = db.Where("narchive_no LIKE ?", "%"+info.Narchive_no+"%")
	}
	if info.Name != "" {
		db = db.Where("name LIKE ?", "%"+info.Name+"%")
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&archiveLocations).Error
	return archiveLocations, total, err
}

// GetArchiveLocationByName 根据ID获取档案存放位置记录
func (archiveLocationService *ArchiveLocationService) GetArchiveLocationByName(name string) (archiveLocation archive.ArchiveLocation, err error) {
	err = global.GVA_DB.Where("name = ?", name).First(&archiveLocation).Error
	return
}
